package com.io.basic;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author Abhishek Choudhury
 * @code.description - This class has been created to check operations using ByteStreams
 * @code.learning 1. Byte Streams operate in 8-bit read write mode for all operations<BR>
 *                2. Byte Stream classes inherit from {@link java.io.InputStream} and {@link java.io.OutputStream}
 *                Classes <BR>
 *                3. For demonstration we are using FileInputStream,FileOutputStream and
 *                DataInputStream,DataOutputStream<BR>
 * 
 * 
 */
public class SimpleByteStreams {

	private static String FILE_LOC = "tempFile";

	/**
	 * @param args
	 */
	public static void main(String[] args) {

//		new SimpleByteStreams().writeBytes();
		new SimpleByteStreams().readBytes();

	}
	/**
	 * @code.description This method writes to a file using FileOutputStream
	 * @code.learning The fis.read() reads only one byte from a FileInputStream at a time
	 */
	private void readBytes() {
		
		FileInputStream fis = null;
		
		try {
			fis = new FileInputStream(new File(FILE_LOC));
			System.out.println(fis.read());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally
		{
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
	}

	/**
	 * @code.description This method writes to a file using FileOutputStream
	 * @code.learning The fos.write(int var) - takes int as an input but write only one of the bytes( after casting the
			 int to byte) in the inputStream
	 */
	public void writeBytes() {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(new File(FILE_LOC));
			// NOTICE: the fos.write(int var) - takes int as an input but write only one of the bytes( after casting the
			// int to byte) in the inputStream
			fos.write(new byte[]{0x30,0x31,0x32,0x33});
			fos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally
		{
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}
